//
//  AddressChooseCell.m
//  KuWoCan
//
//  Created by hwd on 17/3/14.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "AddressChooseCell.h"

@interface AddressChooseCell ()

@property (nonatomic,strong) UILabel *nameLab;

@property (nonatomic,strong) UILabel *teleLab;

@property (nonatomic,strong) UILabel *addressLab;

@property (nonatomic,strong) UIButton *selectBtn;

@end

@implementation AddressChooseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    _nameLab = [UILabel new];
    _nameLab.textColor = [UIColor grayColor];
    _nameLab.font = [UIFont systemFontOfSize:14.0];
    _nameLab.textAlignment = NSTextAlignmentLeft;
    _nameLab.text = @"张三丰";
    [self addSubview:_nameLab];
    [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.height.equalTo(@14);
        make.top.equalTo(@15);
    }];
    
    _teleLab = [UILabel new];
    _teleLab.textColor = [UIColor grayColor];
    _teleLab.font = [UIFont systemFontOfSize:14.0];
    _teleLab.textAlignment = NSTextAlignmentLeft;
    _teleLab.text = @"1864484778";
    [self addSubview:_teleLab];
    [_teleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLab.mas_right).offset(15);
        make.height.equalTo(@14);
        make.top.equalTo(_nameLab.mas_top);
    }];
    
    _addressLab = [UILabel new];
    _addressLab.textColor = [UIColor grayColor];
    _addressLab.numberOfLines = 0;
    _addressLab.font = [UIFont systemFontOfSize:14.0];
    _addressLab.textAlignment = NSTextAlignmentLeft;
    _addressLab.text = @"武汉市 光谷大道 未来之光 7-1107";
    [self addSubview:_addressLab];
    [_addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLab.mas_left);
        make.height.greaterThanOrEqualTo(@14);
        make.top.equalTo(_nameLab.mas_bottom).offset(10);
        make.right.equalTo(@-105 );
    }];
    
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [editBtn addTarget:self action:@selector(editAddress) forControlEvents:UIControlEventTouchUpInside];
    [editBtn setImage:kImg(@"edit") forState:UIControlStateNormal];
    [self addSubview:editBtn];
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-15);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.top.equalTo(@15);
    }];
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteBtn addTarget:self action:@selector(deleteAddress) forControlEvents:UIControlEventTouchUpInside];
    deleteBtn.titleLabel.font = KwFont(14);
    [deleteBtn setTitle:@"删除地址" forState:UIControlStateNormal];
    deleteBtn.backgroundColor = KwBlueColor;
    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    deleteBtn.layer.masksToBounds = YES;
    deleteBtn.layer.cornerRadius = 5;
    [self addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_addressLab.mas_bottom);
        make.right.equalTo(@-15);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    
    _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectBtn addTarget:self action:@selector(setDefaultAddress) forControlEvents:UIControlEventTouchUpInside];
    _selectBtn.titleLabel.font = KwFont(14);
    [_selectBtn setImage:kImg(@"tuoyuan") forState:UIControlStateNormal];
    [_selectBtn setImage:kImg(@"select") forState:UIControlStateSelected];
    [_selectBtn setTitle:@"默认地址" forState:UIControlStateNormal];
    [_selectBtn setTitleColor:KwBlueColor forState:UIControlStateNormal];
    _selectBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    _selectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 40);
    [self addSubview:_selectBtn];
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_addressLab.mas_bottom).offset(10);
        make.left.equalTo(@15);
        make.size.mas_equalTo(CGSizeMake(120, 30));
    }];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = KwGaryColor;
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@1.5);
        make.bottom.equalTo(@0);
        make.top.equalTo(_selectBtn.mas_bottom).offset(13);
    }];
}

- (void)editAddress
{
    [_delegate editAddressWithIndexPath:_indexPath];
}

- (void)deleteAddress
{
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定删除？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [_delegate deleteAddressWithIndexPath:_indexPath];
    }
}

- (void)setDefaultAddress
{
    [_delegate setDefaultAddressWithIndexPath:_indexPath];
}

- (void)setModel:(AddressModel *)model
{
    _model = model;
    _nameLab.text = model.name;
    _addressLab.text = model.region;
    _teleLab.text = model.phone;
    _addressLab.text = [NSString stringWithFormat:@"%@ %@", model.region, model.address];
    
    if ([model.isDefault integerValue] == YES)
    {
        _selectBtn.selected = YES;
    }
    else
    {
        _selectBtn.selected= NO;
    }
}

@end
